# -*- coding: utf-8 -*-

'''
Created on 2010-9-20
'''

class PaginationWrap(object):
    '''
    classdocs
    '''
    def __init__(self,paginator,pageNum,request):
        '''
        pageNum is 1-based
        '''
        self.paginator = paginator
        self.pageNum = pageNum
        self.curPage = self.paginator.page(pageNum)
        self.req = request
        
    def has_next(self):
        return self.curPage.has_next()
    
    def has_previous(self):
        return self.curPage.has_previous()()
    
    def firstPage(self):
        page = {}
        page["pageNum"] = 1
        page["paramStr"] = self._getParamStr(1)
        return page
        
    def lastPage(self):
        page = {}
        page["pageNum"] = self.paginator.num_pages
        page["paramStr"] = self._getParamStr(self.paginator.num_pages)
        return page
        
    def nextPage(self):
        page = {}
        page["pageNum"] = self.curPage.next_page_number
        page["paramStr"] = self._getParamStr(self.curPage.next_page_number())
        return page
        
    def prePage(self):
        page = {}
        page["pageNum"] = self.curPage.previous_page_number
        page["paramStr"] = self._getParamStr(self.curPage.previous_page_number())
        return page
    
    def pages(self):
        pageArray = []
        for i in range(self.paginator.num_pages+1)[1:]:
            page = {}
            page["pageNum"] = i
            page["paramStr"] = self._getParamStr(i) 
            pageArray.append(page)
        return pageArray
    
    def is_paginated(self):
        return self.paginator.num_pages > 1
    
    def _getParamStr(self,pageNum):
        """
        pageNum is 1-based
        """
        querystr = ""
        for k,v in self.req.GET.items():
            if k!="page":
                querystr = querystr + k + "=" + v + "&";
        
        querystr = querystr + "page=" + str(pageNum)
        
        return querystr
        